package com.easybuy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easybuy.pojo.User_address;
import com.easybuy.service.AddressService;

@Controller
@RequestMapping(value = "/address")
public class AddressController {
	
	@Resource
	private AddressService addressService;
	
	@RequestMapping(value = "/viewByuserId")
	@ResponseBody
	public Object viewAddressByName(@RequestParam(value = "userId",required = false)String userId) {
		Integer id = Integer.parseInt(userId);
		List<User_address> list = addressService.selectAddressByUserId(id);
		return list;		
		
	}
	
	@RequestMapping(value = "/viewById")
	@ResponseBody
	public Object viewById(@RequestParam(value = "ID",required = false)String ID) {
		Integer id = Integer.parseInt(ID);
		return addressService.selectAddressById(id);
	}
	
	@RequestMapping(value = "/insert")
	@ResponseBody
	public Object insertNew(User_address address) {
		int updateRows = addressService.insertNew(address);
		return updateRows;
	}
	
	@RequestMapping(value = "/del")
	@ResponseBody
	public Object delAddress(@RequestParam(value = "id",required = false)String id) {
		Integer ID = Integer.parseInt(id);
		int updateRow = addressService.deleteAddress(ID);
		return updateRow;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object updateAddress(User_address address) {
		int updateRows = addressService.updateAddress(address);
		return updateRows;
	}
	
	@RequestMapping(value = "/setdefault")
	@ResponseBody
	public Object setdefaultAddress(@RequestParam(value = "id",required = false)String id,
									@RequestParam(value = "userId",required = false)String userId) {
		Integer addressId = Integer.parseInt(id);
		Integer USER_ID = Integer.parseInt(userId);
		int updateRow = addressService.setdefault(addressId, USER_ID);
		return updateRow;
	}
	
	@RequestMapping(value = "/viewdefault")
	@ResponseBody
	public Object viewDefault(@RequestParam(value = "userId",required = false)String userId) {
		Integer USER_ID = Integer.parseInt(userId);
		return addressService.viewdefault(USER_ID);
		
	}
}
